package com.qhit.jdbc;

import com.qhit.jdbc.bean.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * TODO
 *
 * @author 村头老杨头
 * @version 1.0
 * @date 2021/9/24 14:51
 */
public class App {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
        //创建一个学生对象
        Student student = new Student();
        student.setSname("张三");

        //获取类类型有3中方式
//        方式一
        Class<?> stuClass = Class.forName("com.qhit.jdbc.bean.Student");
//        方式二
        Class<Student> studentClass = Student.class;
//        方式三
        Class<? extends Student> aClass1 = student.getClass();

//        获得真实对象构造方法、
        //无参构造方法对象
        Constructor<?> constructor = stuClass.getConstructor();
        Constructor<?> constructor2 = stuClass.getConstructor(String.class);
        //有参构造方法对象
        Constructor<?> constructor1 = stuClass.getConstructor(String.class, String.class, String.class, Date.class, String.class);
        //构造方法创建对象
        Student stu = (Student)constructor.newInstance();


        //获得真实对象属性
        //sname属性对象
        Field snameField = stuClass.getDeclaredField("sname");
        //获取类型
        System.out.println(snameField.getType());
        //属性名
        System.out.println(snameField.getName());
//        student.setSname("李四");
        //设置访问权限  允许sname访问
        snameField.setAccessible(true);
        snameField.set(student,"李四");
        System.out.println(snameField.get(student));


        // 获得真实对象成员方法
        //setSname方法对象
//        student.setSname("王五");
        Method setSnameMethod = stuClass.getMethod("setSname", String.class);
        setSnameMethod.invoke(student, "王五");
        System.out.println(student.getSname());

        //创建对象
        Student stu1 = (Student)stuClass.newInstance();


//        "com.qhit.jdbc.bean.Student"

    }
}
